$().ready(function() {
    $.validator.addMethod(
            "regex",
            function(value, element, regexp) {
                var re = new RegExp(regexp);
                return this.optional(element) || re.test(value);
            },
            "Nederīga video adrese / Invalid video URL."
    );
    
    $("#addMovieForm").validate({
            rules: {
                    Title_Lv: {required:true, minlength: 1, maxlength: 200},
                    Title_En: {required:true, minlength: 1, maxlength: 200},
                    Title_Original: {required:true, minlength: 1, maxlength: 200},
                    Year: {required:true, number: true, range: [1900, 2016]},
                    Duration: {required:true, number: true},
                    Trailer_Link: {required:true, regex: /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/},
                    Description_Lv: {required:true, minlength: 1, maxlength: 5000},
                    Description_En: {required:true, minlength: 1, maxlength: 5000},
                    Poster: "required"
                }
            }
    );
    
    $("#editMovieForm").validate({
            rules: {
                    Title_Lv: {required:true, minlength: 1, maxlength: 200},
                    Title_En: {required:true, minlength: 1, maxlength: 200},
                    Title_Original: {required:true, minlength: 1, maxlength: 200},
                    Year: {required:true, number: true, range: [1900, 2016]},
                    Duration: {required:true, number: true},
                    Trailer_Link: {required:true, regex: /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/},
                    Description_Lv: {required:true, minlength: 1, maxlength: 5000},
                    Description_En: {required:true, minlength: 1, maxlength: 5000},
                    Poster: "required"
                }
            }
    );
    
    $("#reviewMovieForm").validate({
            rules: {
                    Review: {required:true, minlength: 2, maxlength: 5000}
                }
            }
    );
    
    $("#showForm").validate({
            rules: {
                    Movie_Title: {required:true},
                    Start_time_picker: {required:true},
                    End_time_picker: {required:true},
                    Price: {required:true, number:true}
                }
            }
    );    
    
    $("#auditoriumForm").validate({
            rules: {
                    Auditorium_Name: {required:true, minlength: 2, maxlength: 100},
                    Rows: {required:true, number: true, range: [5, 40]},
                    Seats: {required:true, number: true, range: [5, 50]}
                }
            }
    );    
    
    $("#genreFormNew").validate({
            rules: {
                    Genre_Lv: {required:true, minlength: 2, maxlength: 50},
                    Genre_En: {required:true, minlength: 2, maxlength: 50}
                }
            }
    );   
    
    $("#cinemaForm").validate({
            rules: {
                    Cinema_Name: {required:true, minlength: 2, maxlength: 120},
                    Cinema_Address: {required:true, minlength: 2, maxlength: 200},
                    Cinema_Slogan: {required:true, minlength: 1, maxlength: 500}
                }
            }
    );
    
    $("#profileBlockForm").validate({
            rules: {
                    Password: {required:true}
                }
            }
    );  
    
    $("#Reservation_form").validate({
            rules: {
                    First_name: {required:true},
                    Last_name: {required:true},
                    Email: {required:true}
                }
            }
    );  
    
    $("#loginForm").validate({
            rules: {
                    Username: {required:true},
                    Password: {required:true}
                }
            }
    );  
});